package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import com.itheima.service.ExprService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    /**
     * 分页查询员工信息
     *
     * @param param 查询参数
     * @return 分页查询结果
     */
    @Autowired
    private DeptService deptService;
    @Autowired
    private ExprService exprService;
    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        Page<Emp> page = new Page<>(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
  /*      LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());
        page = page(page, queryWrapper);*/
        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(page);
        // 获取员工集合
        List<Emp> emps = page.getRecords();
        // 获取全部部门数据
        List<Dept> depts = deptService.list();
        emps.forEach(emp -> {
            depts.forEach(dept -> {
                if (dept.getId().equals(emp.getDeptId())) {
                    emp.setDeptName(dept.getName());
                }
            });
        });
        return new PageResult<>(page.getTotal(), emps);
    }

    /**
     * 保存员工信息，并保存员工工作经历信息
     *
     * @param emp 员工信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEmpAndExpr(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            exprService.saveBatch(exprList);
        }
    }

    /**
     * 根据ID查询员工信息
     *
     * @param id 员工ID
     * @return 员工信息
     */
    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(exprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }

    /**
     * 修改员工信息
     *
     * @param emp 员工信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        exprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            exprService.saveBatch(exprList);
        }

    }

    /**
     * 批量删除员工及其工作经历信息
     *
     * @param ids 员工ID集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpByIds(List<Integer> ids) {
        removeBatchByIds(ids);
        exprService.remove(Wrappers.<EmpExpr>lambdaQuery().in(EmpExpr::getEmpId, ids));
    }
}
